package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.verification.VerificationConfig;
import com.yanqu.road.logic.bussiness.config.VerificationConfigBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class VerificationConfigMgr extends TempMgr {

    private static RandomHelper randomHelper = new RandomHelper();

    private static Map<Integer,VerificationConfig> verificationConfigMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        List<VerificationConfig> configList = VerificationConfigBusiness.getConfigList();
        for (VerificationConfig config : configList) {
            verificationConfigMap.put(config.getId(), config);
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static VerificationConfig getVerificationConfig(int id) {
        return verificationConfigMap.get(id);
    }

    public static VerificationConfig randomGetVerification(int systemId) {
        List<VerificationConfig> list = new ArrayList<>();
        for (VerificationConfig config : verificationConfigMap.values()) {
            List<Integer> systemIds = StringUtils.stringToIntegerList(config.getSystemId(), "\\|");
            if (!systemIds.contains(systemId)) {
                continue;
            }
            list.add(config);
        }
        return list.size() == 0 ? null : list.get(randomHelper.next(0, list.size()));
    }

    public static List<Integer> randomGetChooseIdList(int systemId, int questionId, int limit) {
        List<Integer> chooseIdList = new ArrayList<>();
        // 答案的选项
        VerificationConfig verificationConfig = getVerificationConfig(questionId);
        List<Integer> answerIdList = StringUtils.stringToIntegerList(verificationConfig.getAnswer(), "\\|");
        List<Integer> configAnswerId = StringUtils.stringToIntegerList(GameConfig.ROBOT_CHECK_1_ANSWER_DATA, "\\|");
        List<Integer> allChooseIdList = new ArrayList<>();
        for (VerificationConfig config : verificationConfigMap.values()) {
            List<Integer> systemIds = StringUtils.stringToIntegerList(config.getSystemId(), "\\|");
            if (config.getId() == questionId || !systemIds.contains(systemId)) {
                continue;
            }
            List<Integer> list1 = StringUtils.stringToIntegerList(config.getAnswer(), "\\|");
            for (Integer chooseId : list1) {
                if (!configAnswerId.contains(chooseId) || allChooseIdList.contains(chooseId) || answerIdList.contains(chooseId)) {
                    continue;
                }
                allChooseIdList.add(chooseId);
            }
        }
        // 打乱一下
        Collections.shuffle(allChooseIdList);
        while (chooseIdList.size() < limit && allChooseIdList.size() > 0) {
            chooseIdList.add(allChooseIdList.remove(0));
        }
        return chooseIdList;
    }

}
